package com.xianbeiying.app.ckgl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.adapter.CpContentdapter;
import com.xianbeiying.app.ckgl.adapter.CpTypeadapter;
import com.xianbeiying.app.ckgl.bean.CropsBean;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.GetModel;
import com.xianbeiying.app.ckgl.util.StringUtil;

/**
 * 模型种植
 * */
public class ModePlantActivity extends BaseActivity implements View.OnClickListener,HttpCallBackUtil{
    private ImageView iv_title_left;
    private TextView tv_title;
    //育苗
    private LinearLayout ly_mp_ym;
    private RelativeLayout rl_mp_ym;
    private ImageView iv_mp_ym;
    private TextView tv_ym_line;
    //种植
    private LinearLayout ly_mp_zz;
    private RelativeLayout rl_mp_zz;
    private ImageView iv_mp_zz;
    private TextView tv_zz_line;
    private ListView rv_plant_type;
    private GridView rv_plant_content;
    private CpContentdapter cpContentadapter;
    private CpTypeadapter cpTypeadapter;
    private RecyclerView rv_choice_content;
//    private String[] typeStr = {"叶根类", "根茎类", "茄果类", "白菜类", "豆类", "薯芋类", "花菜类", "食用菌", "瓜类", "其他类"};
    private GetModel getModel;
    private int selectId;
    private int plantId;//种植类型id 0 育苗 1种植
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mode_plant);
        initView();
    }

    private void initView() {
        iv_title_left = (ImageView) findViewById(R.id.iv_title_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("模型种植");
        getModel=new GetModel(this);
        getModel.addResponseListener(this);
        //育苗
        ly_mp_ym=(LinearLayout) findViewById(R.id.ly_mp_ym);
        rl_mp_ym=(RelativeLayout) findViewById(R.id.rl_mp_ym);
        iv_mp_ym=(ImageView) findViewById(R.id.iv_mp_ym);
        tv_ym_line=(TextView) findViewById(R.id.tv_ym_line);
        //种植
        ly_mp_zz=(LinearLayout) findViewById(R.id.ly_mp_zz);
        rl_mp_zz=(RelativeLayout) findViewById(R.id.rl_mp_zz);
        iv_mp_zz=(ImageView) findViewById(R.id.iv_mp_zz);
        tv_zz_line=(TextView) findViewById(R.id.tv_zz_line);
        rl_mp_ym.setSelected(true);
        tv_ym_line.setSelected(true);
        iv_mp_ym.setVisibility(View.VISIBLE);

        rv_plant_type = (ListView) findViewById(R.id.rv_plant_type);
        rv_plant_content = (GridView) findViewById(R.id.rv_plant_content);
        rv_choice_content = (RecyclerView)findViewById(R.id.rv_choice_content);
        initData();
        initEvent();
    }
    private void initData() {
//        getModel.GETModePlant("",1111,"",true);
        getModel.GETALLCrops("GET_CROPS",true);
    }
    private void initEvent(){
        ly_mp_ym.setOnClickListener(this);
        ly_mp_zz.setOnClickListener(this);
        iv_title_left.setOnClickListener(this);
        rv_plant_content.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent().setClass(ModePlantActivity.this,CropDeatilActivity.class).putExtra("CROP_ID",cropsBean.getV().getALLCROPS().
                        get(selectId).getCrops().get(position).getCropId()).putExtra("CROP_TYPE",cropsBean.getV().getALLCROPS().
                        get(selectId).getCrops().get(position).getCropType()).putExtra("plantId",plantId));
            }
        });
    }
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                //分类
                case R.id.tv_cp_type:
                    int size=cpTypeadapter.getArrayListBoolean().size();
                    for(int i=0;i<size;i++){
                        if(i==msg.arg1){
                            cpTypeadapter.getArrayListBoolean().set(i,true);
                        }else{
                            cpTypeadapter.getArrayListBoolean().set(i,false);
                        }
                    }
                    selectId=msg.arg1;
                    cpTypeadapter.notifyDataSetChanged();
                    cpContentadapter = new CpContentdapter(ModePlantActivity.this,cropsBean.getV().getALLCROPS().get(msg.arg1).getCrops());
                    rv_plant_content.setAdapter(cpContentadapter);
                    break;
            }
        }
    };
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_title_left:
                finish();
                break;
            //育苗
            case  R.id.ly_mp_ym:
                showDefault();
                plantId=0;
                rl_mp_ym.setSelected(true);
                tv_ym_line.setSelected(true);
                iv_mp_ym.setVisibility(View.VISIBLE);
//                getModel.GETModePlant("",1111,"",true);
                break;
            //种植
            case  R.id.ly_mp_zz:
                showDefault();
                plantId=1;
                rl_mp_zz.setSelected(true);
                tv_zz_line.setSelected(true);
                iv_mp_zz.setVisibility(View.VISIBLE);
//                getModel.GETModePlant("",1111,"",true);
                break;
        }
    }
    //设置在默认显示
    private void showDefault(){
        rl_mp_ym.setSelected(false);
        tv_ym_line.setSelected(false);
        rl_mp_zz.setSelected(false);
        tv_zz_line.setSelected(false);
        iv_mp_ym.setVisibility(View.GONE);
        iv_mp_zz.setVisibility(View.GONE);
    }

    private CropsBean cropsBean;
    @Override
    public void onCallBackData(String url, Object content) {
         if(url.equals("农作物")){
             if(content!=null){
                 cropsBean= JSON.parseObject(new String((byte[])content),CropsBean.class);
                 cpContentadapter = new CpContentdapter(this,cropsBean.getV().getALLCROPS().get(0).getCrops());
                 cpTypeadapter = new CpTypeadapter(this,cropsBean);
                 cpTypeadapter.setArrayListBoolean(StringUtil.getCropArryList(cropsBean,0));
                 rv_plant_type.setAdapter(cpTypeadapter);
                 rv_plant_content.setAdapter(cpContentadapter);
                 cpTypeadapter.setHandler(handler);
             }
         }
    }
}
